package com.shuzhi.stationarysource.modules.job.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @ClassName HttpUtils
 * @Description HttpUtils
 * @Author 孙峰
 * @Date 2020/3/10 12:03
 * @Version 1.0
 */
@Slf4j
public class HttpUtils {
    /**
     * get方式请求
     *
     * @param url
     *            接口服务地址
     * @throws IOException
     */
    public static String getMethod(String url) throws IOException {
        URL restURL = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
        conn.setRequestMethod("GET"); // POST GET PUT DELETE
        conn.setRequestProperty("Accept", "application/json");
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
        StringBuffer buffer = new StringBuffer();
        String str;
        while ((str = br.readLine()) != null) {
            buffer.append(str);
        }
        br.close();
        return buffer.toString();
    }

    /**
     *  新增接口post请求方法
     * @param Info
     * @return
     * @throws IOException
     */
    public static String GetResponse(String Info) throws IOException {

        String path = "http://10.100.244.160:8088/service/serviceinterface/search/run.action?interfaceId=FE8FA9EA819C4943AF29961C458BAF4E&token=9b7202888fdf1de45d415387aae233e9";
        //1, 得到URL对象
        URL url = new URL(path);
        //2, 打开连接
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        //3, 设置提交类型
        conn.setRequestMethod("POST");
        //4, 设置允许写出数据,默认是不允许 false
        conn.setDoOutput(true); conn.setDoInput(true);
        //当前的连接可以从服务器读取内容, 默认是true
        //5, 获取向服务器写出数据的流
        OutputStream os = conn.getOutputStream();
        //参数是键值队 , 不以"?"开始
        os.write(Info.getBytes("utf-8"));
//		os.write("entername=123".getBytes());
        os.flush();
        //6, 获取响应的数据
        //得到服务器写回的响应数据
        BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
        String str = br.readLine();
        log.info("响应内容为: " + str);
        return str;
    }

    /**
     * post方式请求
     *
     * @param url
     *            服务地址
     * @param query
     *            json格式参数
     * @throws IOException
     */
    public static String postData(String url, String query) throws IOException {
        URL restURL = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);
        PrintStream ps = new PrintStream(conn.getOutputStream());
        ps.print(query);
        ps.close();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        StringBuffer buffer = new StringBuffer();
        while ((line = br.readLine()) != null) {
            buffer.append(line);
        }
        br.close();

        return buffer.toString();
    }
}
